package mycinema.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import mycinema.dao.CategoryDao;
import mycinema.dao.MovieDao;
import mycinema.dao.impl.CategoryDaoImpl;
import mycinema.dao.impl.MovieDaoImpl;
import mycinema.entity.Category;
import mycinema.entity.Movie;

public class MoviePageServlet extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		CategoryDao categoryDao = new CategoryDaoImpl();
		List<Category> categoryList = categoryDao.getAll();
		MovieDao movieDao = new MovieDaoImpl();
		int pageSize = 5;
		int pageNum = 1;
		String title = "";
		int cid = 0;
		if(request.getParameter("page")!=null &&!request.getParameter("page").isEmpty())
			pageNum =Integer.parseInt(request.getParameter("page"));
		if(request.getParameter("title")!=null )
			title = request.getParameter("title");
		if(request.getParameter("cid")!=null && !request.getParameter("cid").isEmpty())
			cid = Integer.parseInt(request.getParameter("cid"));
		int rows = movieDao.getMovieRows(cid, title);
		int totalPage =(int) Math.ceil((double)rows/pageSize);
		List<Movie> moviesList = movieDao.getMovies(cid, title, pageSize, pageNum);
		
		request.setAttribute("movie", moviesList);
		request.setAttribute("category", categoryList);
		
		request.getRequestDispatcher("MoviePage.jsp").forward(request, response);
	}

}
